package selenium;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class testtudou {
//土豆采集基本完成
	public static void main(String[] args) {
		final WebDriver driver = new ChromeDriver();
		try {
			driver.manage().window().maximize();
			driver.get("http://www.tudou.com/sec/%E7%BB%BC%E8%89%BA?spm=a2h28.8313475.nav.dn_sec6");//链接采集
			System.out.println("aaa");
			for (int i = 0; i < 500; i++) {
				//方法一： 
				  //1、操作垂直滚动条，向下移动500像素
				   String setscroll = "document.documentElement.scrollTop=" + "9999999999";  
				   //2、水平滚动条，向右移动500像素
				   //String setscroll = "document.documentElement.scrollLeft=" + "500";  
				   
				   JavascriptExecutor jse=(JavascriptExecutor) driver;  
				   jse.executeScript(setscroll); 
			}
			System.out.println("bbb");
			List<WebElement> aaa = driver.findElements(By.className("v-thumb__link"));
			int zongshu = aaa.size();
			int a =0;
			
			Date now = new Date();//获取系统当前时间
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//格式化日期，和数据库中日期格式相同，这里是datetime
			String strDate = s.format(now);//格式化日期转换为字符串
			
			

			
			for (WebElement aaElement : aaa) {
				String urls = aaElement.getAttribute("href");
				String url=urls.split("\\?")[0];
				a+=1;
				System.out.println(a+"_排序_"+zongshu+url);
				method2("E:\\"+strDate+"【"+zongshu+"】"+".txt", url);
			};

		} catch (Exception e) {
			e.printStackTrace();
		}
		//driver.quit();
	}

	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
